package com.battleship.util;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.battleship.CommonUtilities;
import com.battleship.InternetGameActivity;
import com.battleship.R;

public class ImageAdapter extends BaseAdapter {

	private Activity mContext;
	private int DEFAULT_PADDING = 1, CELLS_NUMBER = 11, _cellSize;
	public Integer[] mId;

	public ImageAdapter(Activity c) {
		mContext = c;
		_cellSize = getCellDimension();

		mId = new Integer[100];
		for (int i = 0; i < mId.length; i++) {
			mId[i] = R.drawable.water;
		}
	}

	@Override
	public int getCount() {
		return 121;
	}

	@Override
	public Object getItem(int position) {
		return mId[position];
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	private int getCellDimension() {
		DisplayMetrics displaymetrics = new DisplayMetrics();
		mContext.getWindowManager().getDefaultDisplay()
				.getMetrics(displaymetrics);
		int width = displaymetrics.widthPixels;

		width = width - (DEFAULT_PADDING * CELLS_NUMBER);
		return width / CELLS_NUMBER;

	}

	private void cellReceiver(Cell cell){
		if(mContext instanceof InternetGameActivity){
			Toast.makeText(mContext, "Celda: " + cell.getLetter() + String.valueOf(cell.getColumnNumber()), Toast.LENGTH_SHORT).show();
		}
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (isImageForGetView(position)) {
			ImageView imageView = new ImageView(mContext);
			if (position > 0){
				Cell newCell = new Cell(position);
				imageView.setTag(newCell);
				imageView.setImageResource(R.drawable.water);
				imageView.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Cell cell = (Cell)v.getTag();
						cellReceiver(cell);
					}
				});
			}
			imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
			imageView.setLayoutParams(new GridView.LayoutParams(_cellSize,
					_cellSize));
			imageView.setPadding(DEFAULT_PADDING, DEFAULT_PADDING,
					DEFAULT_PADDING, DEFAULT_PADDING);
			imageView.setBackgroundColor(000000);
			return imageView;
		} else {
			String text = null;
			if (position <= 10) {
				text = String.valueOf(position);
			} else {
				text = getLetterForGetView(position);
			}
			
			RelativeLayout rl = new RelativeLayout(mContext);
			rl.setLayoutParams(new GridView.LayoutParams(_cellSize, _cellSize));
			
			RelativeLayout.LayoutParams rlParams =new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			rlParams.addRule(RelativeLayout.CENTER_IN_PARENT);
			
			TextView txt = new TextView(mContext);

			txt.setClickable(false);
			txt.setLayoutParams(rlParams);
			txt.setText(text);
			txt.setTextSize(_cellSize/2);
			rl.setPadding(DEFAULT_PADDING, DEFAULT_PADDING, DEFAULT_PADDING,
					DEFAULT_PADDING);
			rl.addView(txt);
			return rl;
		}

	}

	private String getLetterForGetView(int number) {
		String text = null;
		switch (number) {
		case CommonUtilities.A:
			text = "A";
			break;
		case CommonUtilities.B:
			text = "B";
			break;
		case CommonUtilities.C:
			text = "C";
			break;
		case CommonUtilities.D:
			text = "D";
			break;
		case CommonUtilities.E:
			text = "E";
			break;
		case CommonUtilities.F:
			text = "F";
			break;
		case CommonUtilities.G:
			text = "G";
			break;
		case CommonUtilities.H:
			text = "H";
			break;
		case CommonUtilities.I:
			text = "I";
			break;
		case CommonUtilities.J:
			text = "J";
			break;
		default:
			text = "?";
		}
		return text;
	}

	private boolean isImageForGetView(int number) {
		boolean result;
		if (number <= 10 && number > 0)
			result = false;
		else if (number == 0)
			result = true;
		else {
			switch (number) {
			case CommonUtilities.A:
			case CommonUtilities.B:
			case CommonUtilities.C:
			case CommonUtilities.D:
			case CommonUtilities.E:
			case CommonUtilities.F:
			case CommonUtilities.G:
			case CommonUtilities.H:
			case CommonUtilities.I:
			case CommonUtilities.J:

				result = false;
				break;
			default:
				result = true;
			}
		}
		return result;
	}

	private class Cell {
		private int _columnNumber;
		private String _letter;

		public Cell(int number) {
			
			if (number <= 21) {
				_letter = "A";
				_columnNumber = number - CommonUtilities.A ;
			} else if (number <= 32) {
				_letter = "B";
				_columnNumber = number - CommonUtilities.B ;
			} else if (number <= 43) {
				_letter = "C";
				_columnNumber = number - CommonUtilities.C ;
			} else if (number <= 54) {
				_letter = "D";
				_columnNumber = number - CommonUtilities.D ;
			} else if (number <= 65) {
				_letter = "E";
				_columnNumber = number - CommonUtilities.E ;
			} else if (number <= 76) {
				_letter = "F";
				_columnNumber = number - CommonUtilities.F ;
			} else if (number <= 87) {
				_letter = "G";
				_columnNumber = number - CommonUtilities.G ;
			} else if (number <= 98) {
				_letter = "H";
				_columnNumber = number - CommonUtilities.H ;
			} else if (number <= 109) {
				_letter = "I";
				_columnNumber = number - CommonUtilities.I ;
			} else {
				_letter = "J";
				_columnNumber = number - CommonUtilities.J ;
			}
			
		}

		public String getLetter() {
			return this._letter;
		}

		public int getColumnNumber() {
			return this._columnNumber;
		}
	}
}
